unit MiscTopicF;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TopicF, ComCtrls, DBXpress, WideStrings, DB, SqlExpr, StdCtrls,
  FMTBcd, Grids, DBGrids, DBGridA, DBClient, Provider, ProgramM, DBCtrls,
  LibXmlParser;

type
  TMiscTopicFrame = class(TTopicFrame)
    Tree: TTreeView;
    procedure TreeKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure TreeClick(Sender: TObject);
  private
    procedure LoadQueries;
    procedure ParseQueryHeader(Filename: String; out Title: String; out Section: String);
    function GetTreeSection(Name: String): TTreeNode;
  public
    procedure OnShow; override;
  end;

var
  MiscTopicFrame: TMiscTopicFrame;

implementation
uses
  PReport, Utils, MainFrm;

{$R *.dfm}

{ TMiscTopicFrame }

function TMiscTopicFrame.GetTreeSection(Name: String): TTreeNode;
var
  i : Integer;
begin
  Result := Tree.Items.GetFirstNode;

  while Result <> nil do
  begin
    if Result.Text = Name then
    begin
      Exit;
    end
    else
    begin
      Result := Result.GetNextSibling;
    end;
  end;

  // ei leidnud sobivat, teen uue
  Result := Tree.Items.AddChild(nil, Name);
  Result.ImageIndex := 1;
  Result.SelectedIndex := 1;
end;

procedure TMiscTopicFrame.LoadQueries;
var
  Files: TStringList;
  i: Integer;
  Id: Integer;
  Filename: String;
  Title: String;
  Section: String;
  Node: TTreeNode;
begin
  Tree.Items.BeginUpdate;
  Tree.Items.Clear;

  Files := TStringList.Create;
  FindFiles(GetAbsoluteName('Queries'), Files, 'q*.xml', True);

  for i := 0 to Files.Count-1 do
  begin
    Filename := ExtractFileName(Files[i]);
    Id := StrToInt(Copy(Filename, 2, Length(Filename)-5));
    ParseQueryHeader(Files[i], Title, Section);
    Node := Tree.Items.AddChildObject(GetTreeSection(Section), Title, Pointer(Id));
    Node.ImageIndex := 2;
    Node.SelectedIndex := 2;
    GetTreeSection(Section).Expand(False);
  end;
  Files.Free;

  Tree.AlphaSort;
  Tree.Items.EndUpdate;
end;

procedure TMiscTopicFrame.OnShow;
begin
  if Tree.Items.Count = 0 then
  begin
    LoadQueries;
  end;
end;

procedure TMiscTopicFrame.ParseQueryHeader(Filename: String; out Title,
  Section: String);
var
  Parser: TXmlParser;
begin
  Parser := TXmlParser.Create;
  Parser.LoadFromFile(Filename);

  while Parser.Scan do
  begin
    if Parser.CurPartType = ptStartTag then
    begin
      Assert(Parser.CurName = 'paring');
      Title := Parser.CurAttr.Value('pealkiri');
      Section := Parser.CurAttr.Value('jaotus');
      Break;
    end;
  end;

  Parser.Free;
end;

procedure TMiscTopicFrame.TreeClick(Sender: TObject);
begin
  if (Tree.Selected <> nil) and (Tree.Selected.Data <> nil) then
  begin
//    ShowMessage(Tree.Selected.Text);
    MainForm.OpenQuery(Integer(Tree.Selected.Data));
  end;
end;

procedure TMiscTopicFrame.TreeKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_F5 then
  begin
    LoadQueries;
  end
  else if Key = VK_RETURN then
  begin
    TreeClick(Self);
  end;
end;

{
procedure TMiscTopicFrame.UpdateQueriesTable;
var
  ExistingIDs: String;
  Files: TStringList;
  i: Integer;
  Id: Integer;
  Filename: String;
  Title: String;
  Section: String;
begin
  Files := TStringList.Create;
  FindFiles(GetAbsoluteName('Queries'), Files, 'q*.xml', True);

  ExistingIDs := '';
  for i := 0 to Files.Count-1 do
  begin
    Filename := ExtractFileName(Files[i]);
    Id := StrToInt(Copy(Filename, 2, Length(Filename)-5));

    ParseQueryHeader(Files[i], Title, Section);
    DBQuery('update or insert into PARINGUD (id, pealkiri, jaotus) '
      + 'values (%d, %s, %s)',
      [Id, DBQuoteString(Title), DBQuoteString(Section)]);

    ExistingIDs := ExistingIDs + IntToStr(Id) + ',';
  end;
  Files.Free;

  DBQuery('delete from paringud where id < 1000 and id not in ('
    + TrimTrailingSubstring(ExistingIDs, ',') + ')');
end;
}
end.
